﻿using AnalyStock.DataManage;
using AnalyStock.GrapImage;
using Microsoft.Win32;

namespace AnalyStock.WPFPage;

public partial class LookStockPage : Page
{
    public LookStockPage()
    {
        InitializeComponent();
        ChartKline.StockInfosFilled += Bakdailes_Filled;
        CBoxSeleStockByMarket.SelectionChanged += CBoxSeleStockByMarket_SelectionChanged;
        CBoxSelcStockByIndustry.SelectionChanged += CBoxSeleStockByIndustry_SelectionChanged;
        BtnLoadSeleOfCSV.Click += BtnLoadSeleOfCSV_Click;
        DViewStockInfor.SelectionChanged += SeleStock_Changed; //DViewStockInfor_SelectionChanged;
        DViewStockInfor.KeyUp += SeleStock_Changed; //DViewStockInfor_KeyUp;       
        ChartKline.StockInfosFilled += Bakdailes_Filled;
    }

    private void Bakdailes_Filled(object sender, IList<StockInfo> stockInfos)
    {

        DViewStockInfor.ItemsSource = stockInfos.OrderBy(n => n.Ts_code);
        CBoxSeleStockByMarket.ItemsSource = stockInfos.Select(n => n.Market)
                                                      .Distinct()
                                                      .Concat(new[] { "自选股", "全部股" });
        CBoxSelcStockByIndustry.ItemsSource = stockInfos.Select(n => n.Industry)
                                                        .Distinct();
    }

    /// <summary>
    ///     其他数据处理方法
    /// </summary>
    /// <param name="csvPathFile"></param>
    /// <returns></returns>
    private void BtnLoadSeleOfCSV_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog dialog = new()
        {
            Multiselect = true, //该值确定是否可以选择多个文件
            Title = "请选择文件夹",
            Filter = "CSV文件(*.CSV,*.csv)|*.CSV;*.csv"
        };
        var result = dialog.ShowDialog();
        if (result is true)
        {
            var nameFile = dialog.FileName;
            var tmpDt = LoadDataofCsvFile(nameFile);
            if (tmpDt.Rows.Count < 0)
            {
                return;
            }

            List<StockInfo> stockInforOnLoad = new();
            foreach (DataRow dr in tmpDt.Rows)
            {
                stockInforOnLoad.Add(new StockInfo
                {
                    Ts_code = dr.Field<string>(0),
                    Name = dr.Field<string>(1)
                });
            }

            DViewStockInfor.ItemsSource = stockInforOnLoad.OrderBy(n => n.Ts_code);
        }
    }

    public static DataTable LoadDataofCsvFile(string csvPathFile)
    {
        try
        {
            return CsvDataManage.ReadDataofCSV(csvPathFile);
        }
        catch (Exception ex)
        {
            ex.Message.ErrorMessage();
        }

        return new DataTable();
    }

    /// <summary>
    ///     K线数据筛选变化
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>

    #region

    private void SeleStock_Changed(object sender, EventArgs e)
    {
        if (DViewStockInfor.CurrentItem is StockInfo currentStock)
        {
            ChartKline.ChangeCurrentStock(currentStock);
            return;
        }
        if (DViewStockInfor.CurrentItem is SelectStock currentSelect)
        {
            ChartKline.ChangeCurrentStock(currentSelect.Ts_code);
            return;
        }
    }

    private void CBoxSeleStockByMarket_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        GetDViewDataSource(FilterType.market);
    }

    private void CBoxSeleStockByIndustry_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        GetDViewDataSource(FilterType.industry);
    }

    private enum FilterType
    {
        industry,
        market,
        selfselect
    }

    private void GetDViewDataSource(FilterType filterType)
    {
        DViewStockInfor.ItemsSource = default;
        switch (filterType)
        {
            case FilterType.industry:
                DViewStockInfor.ItemsSource = ChartKline.GetAllStockInfos()
                    .Where(n => n.Industry == (string)CBoxSelcStockByIndustry.SelectedItem).OrderBy(n => n.Ts_code);
                break;
            case FilterType.market:
                DViewStockInfor.ItemsSource = (string)CBoxSeleStockByMarket.SelectedItem switch
                {
                    "自选股" => ChartKline.GetSelectStocks().OrderBy(n => n.Conception),
                    "全部股" => ChartKline.GetAllStockInfos().OrderBy(n => n.Ts_code),
                    _ => ChartKline.GetAllStockInfos()
                        .Where(n => n.Market == (string)CBoxSeleStockByMarket.SelectedItem).OrderBy(n => n.Ts_code)
                };
                break;
        }
    }

    #endregion
}